package cc.mcggdd.leah.boot.stock.service.impl;

import cc.mcggdd.leah.boot.core.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.mcggdd.leah.boot.stock.mapper.StockBondReverseRepoMapper;
import cc.mcggdd.leah.boot.stock.service.StockBondReverseRepoService;
import cc.mcggdd.leah.boot.stock.model.entity.StockBondReverseRepo;
import cc.mcggdd.leah.boot.stock.model.form.StockBondReverseRepoForm;
import cc.mcggdd.leah.boot.stock.model.query.StockBondReverseRepoQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockBondReverseRepoVO;
import cc.mcggdd.leah.boot.stock.converter.StockBondReverseRepoConverter;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 国债逆回购服务实现类
 *
 * @author 
 * @since 2025-02-08 12:39
 */
@Service
@RequiredArgsConstructor
public class StockBondReverseRepoServiceImpl extends ServiceImpl<StockBondReverseRepoMapper, StockBondReverseRepo> implements StockBondReverseRepoService {

    private final StockBondReverseRepoConverter stockBondReverseRepoConverter;

    /**
    * 获取国债逆回购分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<StockBondReverseRepoVO>} 国债逆回购分页列表
    */
    @Override
    public IPage<StockBondReverseRepoVO> getStockBondReverseRepoPage(StockBondReverseRepoQuery queryParams) {
        Page<StockBondReverseRepoVO> pageVO = this.baseMapper.getStockBondReverseRepoPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取国债逆回购表单数据
     *
     * @param id 国债逆回购ID
     * @return
     */
    @Override
    public StockBondReverseRepoForm getStockBondReverseRepoFormData(Long id) {
        StockBondReverseRepo entity = this.getById(id);
        return stockBondReverseRepoConverter.toForm(entity);
    }
    
    /**
     * 新增国债逆回购
     *
     * @param formData 国债逆回购表单对象
     * @return
     */
    @Override
    public boolean saveStockBondReverseRepo(StockBondReverseRepoForm formData) {
        StockBondReverseRepo entity = stockBondReverseRepoConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }
    
    /**
     * 更新国债逆回购
     *
     * @param id   国债逆回购ID
     * @param formData 国债逆回购表单对象
     * @return
     */
    @Override
    public boolean updateStockBondReverseRepo(Long id,StockBondReverseRepoForm formData) {
        StockBondReverseRepo entity = stockBondReverseRepoConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }
    
    /**
     * 删除国债逆回购
     *
     * @param ids 国债逆回购ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteStockBondReverseRepos(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的国债逆回购数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public BigDecimal getStockBondReverseRepoTotalAmount() {
        BigDecimal totalAmount = this.baseMapper.getStockBondReverseRepoTotalAmount();
        if(totalAmount==null){
            totalAmount = BigDecimal.valueOf(0.00);
        }
        return totalAmount;
    }

}
